package com.ruoyi.ai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ai.domain.bo.WxDialogueBo;
import com.ruoyi.ai.domain.vo.WxDialogueVo;
import com.ruoyi.ai.domain.WxDialogue;
import com.ruoyi.ai.mapper.WxDialogueMapper;
import com.ruoyi.ai.service.IWxDialogueService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 对话管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-01
 */
@RequiredArgsConstructor
@Service
public class WxDialogueServiceImpl implements IWxDialogueService {

    private final WxDialogueMapper baseMapper;

    /**
     * 查询对话管理
     */
    @Override
    public WxDialogueVo queryById(Long dialogueId) {
        return baseMapper.selectVoById(dialogueId);
    }

    /**
     * 查询对话管理列表
     */
    @Override
    public TableDataInfo<WxDialogueVo> queryPageList(WxDialogueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxDialogue> lqw = buildQueryWrapper(bo);
        Page<WxDialogueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询对话管理列表
     */
    @Override
    public List<WxDialogueVo> queryList(WxDialogueBo bo) {
        LambdaQueryWrapper<WxDialogue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxDialogue> buildQueryWrapper(WxDialogueBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxDialogue> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWxUserId() != null, WxDialogue::getWxUserId, bo.getWxUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getFirstContent()), WxDialogue::getFirstContent, bo.getFirstContent());
        lqw.eq(bo.getIsShow() != null, WxDialogue::getIsShow, bo.getIsShow());
        lqw.orderByDesc(WxDialogue::getCreateTime);
        return lqw;
    }

    /**
     * 新增对话管理
     */
    @Override
    public Boolean insertByBo(WxDialogueBo bo) {
        bo.setIsShow(true);
        if (bo.getFirstContent() == null) {
            bo.setFirstContent("");
        }
        WxDialogue add = BeanUtil.toBean(bo, WxDialogue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDialogueId(add.getDialogueId());
        }
        return flag;
    }

    /**
     * 修改对话管理
     */
    @Override
    public Boolean updateByBo(WxDialogueBo bo) {
        WxDialogue update = BeanUtil.toBean(bo, WxDialogue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WxDialogue entity) {
        //TODO 做一些数据校验,如唯一约束
        entity.setDelFlag(UserConstants.DEL_FLAG_FALSE);
    }

    /**
     * 批量删除对话管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean notShow(List<Long> ids) {
        int update = baseMapper.update(new UpdateWrapper<WxDialogue>()
            .lambda()
            .set(WxDialogue::getIsShow, false)
            .in(WxDialogue::getDialogueId, ids));
        return update > 1;
    }
}
